package com.zhongxi.guoyuan.view.beautifulStore;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.adapter.beautifulStore.HotServiceAdapter;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.base.BaseActivity;
import com.zhongxi.guoyuan.entity.BaseResponse;
import com.zhongxi.guoyuan.entity.coinStore.HotGoodsData;
import com.zhongxi.guoyuan.entity.coinStore.HotGoodsResult;
import com.zhongxi.guoyuan.url.Url;

import java.util.ArrayList;
import java.util.List;

public class StoreJDActivity extends BaseActivity implements View.OnClickListener {

    private PullToRefreshListView lvList;
    private List<HotGoodsData> data = new ArrayList<>();
    private HotServiceAdapter adapter;
    private HotGoodsResult hotServiceResult;

    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.activity_store_jd);
        initTitle();
        tvTitle.setText("热推服务");
        lvList = (PullToRefreshListView) findViewById(R.id.lvList);
    }

    @Override
    protected void initData() {
        super.initData();
        adapter = new HotServiceAdapter(context, data);
        lvList.setAdapter(adapter);
        getServer(Url.HOT_SERVICE, null, "", false, null, "get");
    }

    @Override
    protected void ProcessFailResult(String response) {
        super.ProcessFailResult(response);
        Gson gson = new Gson();
        BaseResponse result = gson.fromJson(response, BaseResponse.class);
        if(null!=result.getSafeobj()&&!"".equals(result.getSafeobj()))
            MyApplication.getInstance().setSpValue(result.getSafeobj());
    }

    @Override
    protected void processSuccessResult(String response) {
        super.processSuccessResult(response);
        Gson gson = new Gson();
        hotServiceResult = gson.fromJson(response, HotGoodsResult.class);
        MyApplication.getInstance().setSpValue(hotServiceResult.getSafeobj());
        if(hotServiceResult.getData().size()>0){
            data.addAll(hotServiceResult.getData());
        }
        adapter.notifyDataSetChanged();
        lvList.onRefreshComplete();
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        ivBack.setOnClickListener(this);
        lvList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(context, ProjectDetailActivity.class);
                intent.putExtra("id", hotServiceResult.getData().get(position-1).getId());
                startActivity(intent);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ivBack:
                finish();
                break;
        }
    }

}
